import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:ljy_p_app/diffwidget/ljy_card_item.dart';
import 'package:ljy_p_app/diffwidget/toast.dart' as LJToast;
import 'package:ljy_p_app/localization/localization_key.dart';
import 'package:ljy_p_app/pages/common/user_profile_info_controller.dart';
import 'package:ljy_p_app/style/lj_style.dart';
import 'package:ljy_p_app/utils/common_utils.dart';

class UserProfileInfo extends StatefulWidget {
  const UserProfileInfo({super.key});

  @override
  State<UserProfileInfo> createState() => _UserProfileInfoState();
}

class _UserProfileInfoState extends State<UserProfileInfo> {

  final UserProfileInfoController _controller = Get.put(UserProfileInfoController());

  _renderItem(IconData leftIcon, String title, RxString value, VoidCallback onPressed) {
    return LjyCardItem(child: GestureDetector(
      onTap: onPressed,
      child: Container(
        height: 80,
        decoration: BoxDecoration(color: Colors.white, borderRadius: BorderRadius.all(Radius.circular(12))),
        child: Center(
          child: Row(
            children: [
              SizedBox(width: 15,),
              Icon(leftIcon, color: LJColors.subTextColor, size: 30,),
              SizedBox(width: 10,),
              Text(title, style: LJConstant.normalSubText),
              SizedBox(width: 10,),
              Expanded(child: Obx(() => Text(value.value, style: LJConstant.normalText, textAlign: TextAlign.left,))),
            ],
          ),
        ),
      ),
    ));
  }

  List<Widget>  _renderList() {
    return [
      _renderItem(Icons.info, LocalizationKey.userProfileName.tr, _controller.userName, () {
        _showEditDialog(LocalizationKey.userProfileName.tr, _controller.userName.value, 'name');
      }),
      _renderItem(Icons.email, LocalizationKey.userProfileEmail.tr, _controller.userEmail, () {
        _showEditDialog(LocalizationKey.userProfileEmail.tr, _controller.userEmail.value, 'email');
      }),
      _renderItem(Icons.link, LocalizationKey.userProfileLink.tr, _controller.userLink, () {
        _showEditDialog(LocalizationKey.userProfileLink.tr, _controller.userLink.value, 'link');
      }),
      _renderItem(Icons.group, LocalizationKey.userProfileOrg.tr, _controller.userOrg, () {
        _showEditDialog(LocalizationKey.userProfileOrg.tr, _controller.userOrg.value, 'org');
      }),
      _renderItem(Icons.location_on, LocalizationKey.userProfileLocation.tr, _controller.userLocation, () {
        _showEditDialog(LocalizationKey.userProfileLocation.tr, _controller.userLocation.value, 'location');
      }),
      _renderItem(Icons.message, LocalizationKey.userProfileInfo.tr, _controller.userInfo, () {
        _showEditDialog(LocalizationKey.userProfileInfo.tr, _controller.userInfo.value, 'info');
      }), 
    ];
  }

  _showEditDialog(String title, String? value, String key) {
    String content = value ?? '';
    CommonUtils.showEditDialog(context, title, (title){}, (res){
      content = res;
    }, (){
      if (content.isEmpty) {
        LJToast.showToast(LocalizationKey.feedBackTip.tr);
        return;
      }
      _controller.updateText(key, content);
      Navigator.of(context).pop();
    },
    titleController: TextEditingController(),
    valueController: TextEditingController(text: value),
    needTitle: false);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: LJColors.primaryValue,
        foregroundColor: LJColors.textColorWhite,
        title: Hero(tag: 'home_user_info', child: Material(
          color: Colors.transparent,
          child: Text(LocalizationKey.homeUserInfo.tr, style: TextStyle(fontSize: 18, color: Colors.white)),
        )),
      ),
      body: Container(
        color: Colors.white,
        child: SingleChildScrollView(
          child: Column(
            children: _renderList(),
          ),
        ),
      ),
    );
  }
}